﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using System.Collections;

namespace DevDemo
{
    public partial class UC_DrugLists_ListView : UserControl
    {
        public UC_DrugLists_ListView()
        {
            InitializeComponent();
        }

        public string TypeName = null;

        private string SelectControlTagID = "";

        public List<DrugClass> drugobjs = new List<DrugClass>();

        public delegate void SetDeleteControls(string TypeCode);

        public event SetDeleteControls SetDelMethod;

        /// <summary>
        /// 
        /// </summary>
        public void GetTableLayOutData()
        {
            this.listView1.BeginUpdate();

            ColumnHeader ch = new ColumnHeader();

            ch.Width = this.listView1.Width - 5;

            ch.Text = "Col";

            this.listView1.Columns.Add(ch);

            this.listView1.EndUpdate();

            ListViewItem rowItem = null;

            ListViewItem.ListViewSubItem subitem = null;

            foreach (var item in this.drugobjs)
            {
                rowItem = new ListViewItem();

                rowItem.Name = item.DrugID;

                subitem = new ListViewItem.ListViewSubItem();

                subitem.Text = item.DrugName;

                subitem.Name = item.DrugName;

                rowItem.SubItems.Insert(0, subitem);

                this.listView1.Items.Add(rowItem);
            }

            ImageList il = new ImageList();
            //设置高度
            il.ImageSize = new Size(1, 30);
            //绑定listView控件
            this.listView1.SmallImageList = il;
        }

        /// <summary>
        /// 右键点击获取值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gb_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                var obj = (UC_DrugList)sender;

                SelectControlTagID = obj.Tag.ToString();
            }
        }

        /// <summary>
        /// 双击项目
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void obj_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        /// <summary>
        /// 删除控件方法 row
        /// </summary>
        /// <param name="items"></param>
        /// <param name="tagId"></param>
        public void RemoveControls(Control items, TableLayoutPanel table, string tagId)
        {
            foreach (var item in items.Controls)
            {
                if (item.GetType().ToString().ToUpper() == "DEVDEMO.UC_DRUGLIST")
                {
                    var b = (UC_DrugList)item;

                    if (b.Tag.ToString() == tagId)
                    {
                        int rowIndex = table.GetRow(b);

                        table.Controls.RemoveAt(rowIndex);

                        // 移动,当前行row的下行往上移动
                        for (int k = rowIndex; k < table.RowCount - 1; k++)
                        {
                            Control ctlNext = table.GetControlFromPosition(0, k + 1);

                            table.SetCellPosition(ctlNext, new TableLayoutPanelCellPosition(0, k));
                        }

                        table.RowStyles.RemoveAt(table.RowCount - 1);

                        table.RowCount = table.RowCount - 1;

                        break;
                    }
                }
                else
                {
                    RemoveControls((Control)item, table, tagId);
                }
            }

            // 重新计算高度，否则最后一行偏大
            table.Height = table.RowCount * 80;
        }

        /// <summary>
        /// 加载操作
        /// </summary>
        public void DropDownButtonShow()
        {
            DXPopupMenu menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("新增", OnItemAddClick));
            menu.Items.Add(new DXMenuItem("修改", OnItemEditClick));
            menu.Items.Add(new DXMenuItem("删除", OnItemDelClick));
            this.dpdb_Edit.DropDownControl = menu;
        }

        private void OnItemAddClick(object sender, EventArgs e)
        {
            DXMenuItem item = sender as DXMenuItem;

            XtraMessageBox.Show("Add" + item.Caption);
        }

        private void OnItemEditClick(object sender, EventArgs e)
        {
            DXMenuItem item = sender as DXMenuItem;

            XtraMessageBox.Show("Edit" + item.Caption);
        }

        private void OnItemDelClick(object sender, EventArgs e)
        {
            //DXMenuItem item = sender as DXMenuItem;

            //XtraMessageBox.Show("Del" + item.Caption);

            SetDelMethod(this.Tag.ToString());
        }

        private void UC_DrugLists_ListView_Load(object sender, EventArgs e)
        {
            GetTableLayOutData();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dpdb_Edit_Click(object sender, EventArgs e)
        {
            ArrayList ar = new ArrayList();

            foreach (ListViewItem item in this.listView1.Items)
            {
                if (item.Checked)
                {
                    ar.Add(item.Text);
                }
            }

            MessageBox.Show(string.Join(",", (string[])ar.ToArray(typeof(string))));
        }

        #region 单击

        /// <summary>
        /// 单击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        //{
        //    if (e.IsSelected)
        //    {
        //        MessageBox.Show(e.Item.Name, "ItemSelection");
        //    }
        //}

        #endregion

        /// <summary>
        /// Item 双击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);

            if (info != null)
            {
                var item = info.Item as ListViewItem;

                MessageBox.Show(item.SubItems[0].Text, "下医嘱");
            }
        }

        /// <summary>
        /// 右键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);

                if (info != null)
                {
                    var item = info.Item as ListViewItem;

                    SelectControlTagID = item.SubItems[0].Text;
                }
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(SelectControlTagID))
            {
                MessageBox.Show(SelectControlTagID, "右键删除");
            }
        }
    }
}
